

jQuery(function ($) {  
    var user = JSON.parse(localStorage.getItem('user_info')) || {};
    var userId = user.id;
    var onoff = false;
    var current_sort;
    //搜索关键字请求接口渲染页面数据
    var keyword = decodeURIComponent(window.location.href.split('=')[1]);
    if (keyword != 'undefined') {
        $('#location').append('<h1>' + keyword + '</h1>');
        $('#search').attr('value', keyword);
    }
    $('.s_nav li a').click(function(){
        var _this = $(this);
        if(!_this.hasClass('active_color')){
            $('.s_nav li a').removeClass("active_color");
            _this.addClass("active_color");
            if(_this.hasClass('zh')){
                _this.find('i').addClass('down').removeClass('up');
                requestGoodsList(0,0);
                pageOnOff=true;
            }else if(_this.hasClass('jg')){
                pageOnOff=true;
                _this.find('i').addClass('down').removeClass('up');
                requestGoodsList(0,2);
            }else if(_this.hasClass('sj')){
                pageOnOff=true;
                _this.find('i').addClass('down').removeClass('up');
                requestGoodsList(0,1);
            }
        }
        else{
            if(_this.hasClass('jg')){
                pageOnOff=true;
                if(_this.find('i').hasClass('down')){
                    _this.find('i').removeClass('down').addClass('up');
                     requestGoodsList(0,3);
                }else{
                    _this.find('i').addClass('down').removeClass('up');
                    requestGoodsList(0,2);
                }
            }     
        }
    })
    var page = 0;
    //导航栏关键字
    requestGoodsList(page);
    //分页按钮
    var pageOnOff = true;
    function twbsPagination( totalPage) {
        Page({
			num:Math.ceil(totalPage / 20),					//页码数
			startnum:1,				//指定页码
			elem:$('#pagination_b'),		//指定的元素
			callback:function(page){	//回调函数
                if (page != 1) {
                    onoff = true;
                }
                if (onoff) {
                    $('.works_show').empty();
                    requestGoodsList(page - 1,current_sort);
                    $('html,body').animate({scrollTop:$('.s_content ').offset().top-80}, 500);
                }
			}
		});


    };
    function requestGoodsList(page,value) {
        var data = {
            pageNum: page,
            pageSize: 20,
            userId:userId,
            sort:value
        };
        if (keyword != "") {
            data.keyword = keyword;
        }
        current_sort = value;
        $.ajax({
            type: "GET",
            url: search_url + '/search/goods',
            data: data,
            success: function success(res) {
                if (res.code == 200) {
                    var res = res.data;
                    if (res.list.length == 0) {
                        $('.works_show').append('<h3>\u6CA1\u6709\u6570\u636E</h3>');
                    } else {
                        $('.s_nav_right').empty();
                        $('.s_nav_right').append('<span>\u5171\u6709<span>' + res.total + '</span>件商品</span>\n');
                        var html="";
                        for (var i = 0; i < res.list.length; i++) {
                             var a_good =  res.list[i];
                            a_good['mainFlagImgList'] = [];
                            if(a_good.imgsList.length > 0){
                                for(var mi = 0; mi < a_good.imgsList.length; mi++){
                                    if(a_good.imgsList[mi].mainFlag > 0){
                                        a_good['mainFlagImgList'].push(a_good.imgsList[mi]);
                                    }
                                }
                            }
                           
                            html+= '<li class="works_show_item" data-id="' + a_good.id + '" data-title="' + a_good.name + '" data-url="http://static.yczihua.com/images/201802/thumb_img/9638_thumb_G_1517523720728.jpg" data-price="\xA5' + a_good.goodsPrice + '" data-txt="\u610F\u5883\u4F18\u7F8E">\n                                <div class="works_show_pic">\n  <a href="/goods-' + a_good.id + '.html" target="_blank"><img src="' + isImgHttp(a_good.mainFlagImgList[0].thumb) + '"></img></a> \n                                    <i class="goods_status1  ' + (a_good.goodsStatus.goodsStatus == 6 ? 'open' : 'hide') + ' g_status"></i> <i class="goods_status2 ' + (a_good.goodsStatus.goodsStatus == 5 ? 'open' : 'hide') + ' g_status"></i> <i class="goods_status3 ' + (a_good.goodsStatus.goodsStatus == 4 ? 'open' : 'hide') + ' g_status" ></i> <i class="goods_status4  ' + (a_good.goodsStatus == 1 ? 'open' : 'hide') + ' g_status" ></i> <i class="goods_status5 ' + (a_good.goodsStatus == 3? 'open' : 'hide') + ' g_status" ></i> <i class="goods_status6  ' + (a_good.goodsStatus == 2 ? 'open' : 'hide') + ' g_status" ></i>\n                                </div>\n                                <div class="works_show_item_msg">\
                            <div class="works_show_pic_introduce">\
                                <p class="pic_msg">\
                                <a href="/goods-' + a_good.id + '.html" target="_blank" class="goodsBox">\
                                <span class="title">'+ a_good.name + '</span></a></p>\
                                </div>\ \
                                <div class="works_show_price">\n<span>\xA5' + a_good.goodsPrice+'/'+a_good.unitName+ '</span>\n </div>\
                                </div>\</li>';

                            $('.works_show').html(html);
                        }
                        if (pageOnOff) {
                            //分页按钮
                            twbsPagination(res.total);
                            pageOnOff=false;
                        }
                        //渲染已经加入的对比
                        if ($.cookie('goods_list') != undefined) {
                            // $('#compare_box').show()
                            reloadData();
                            var localData = getLocalData();
                            if (localData == undefined) return;
                            for (var i = 0; i < localData.length; i++) {
                                var obj = localData[i];
                                var id = obj.productID;
                                $('[data-id=' + id + ']').find(".contrast").addClass('contrast_se');
                            }
                        };
                        isStore(); //是否已经收藏过的商品
                    }
                }
            }
        });
    };
    //是否已经收藏过的商品
    function isStore() {
      
        if (!userId) {
            return;
        }
        var element = $('.works_show_item');
        var goodsIds = [];
        for (var k = 0; k < element.length; k++) {
            goodsIds.push($(element[k]).attr('data-id'));
        };
        var param = {
            "GoodsCollect[]": {
                "GoodsCollect": {
                    "goods_id{}": goodsIds,
                    "user_id": userId
                }
            }
        };
        send_ajax('post', '/get', param, function callback(result) {
            if (result.code == 200 && result['GoodsCollect[]'] != undefined) {
                var data = result['GoodsCollect[]'];
                for (var j = 0; j < data.length; j++) {
                    $('[data-id=' + data[j].goods_id + ']').find(".collect").addClass('select_collect');
                }
            }
        });
    };

});